<?php /**
 *  【梦想cms】 http://www.lmxcms.com *
 *
 *   不需要验证连接token的  直接继承Action   不在继承HomeAction
 *
 *   匹配控制器index
 *
 *   返回的数据格式
 *   array(
 * m=index
 *     data=array(
 *
 * )
 * )
 *
 */
defined('LMXCMS') or exit();
use Workerman\Lib\Timer;
use GatewayWorker\lib\Gateway;
class IndexAction extends HomeAction{
    public function __construct() {
        parent::__construct();
    }

    /*todo 匹配方法
     *
     * 接收客户发送的json匹配数据
     * 加入临时房间
     *
     * 判断   满员->开车  未满员-> 返回当前匹配数据
     *
     * 开车逻辑：临时房间加入到正式房间并初始化数据
     * 返回客户数据 通知客户进入房间
    */
    public function pipei(){
        //echo "首页控制器";
        $uid=$this->SocketData['uid'];
        $nums=$this->SocketData['num'];//多少人的比赛

        $this->DataModel->setUidtoTemRoom($uid);//todo 加入临时房间
        $roomid=$this->DataModel->getRoomId();
        $temRoomData= $this->DataModel->getTemRoomData();//todo 获取临时房间的数据
        echo 'ttttttttttttttttttt';

        if(count($temRoomData)<$nums){

            echo "send to  index";
            $roomid=$this->DataModel->getRoomId();//todo 获取房间号
            echo "roomid=".$roomid;
            $reData['m']="index";
            $reData['innums']=count($temRoomData);
            $reData['else']=$nums-count($temRoomData);
            $reData['roomid']=$roomid;
            echo var_dump($temRoomData);
            Gateway::sendToUid($temRoomData,json_encode($reData));
        }else{
            echo 'ddddddddddddddddddddddddddddddddddddd';
            foreach($temRoomData as $k=>$v){
                if(!Gateway::isUidOnline($v)){
                    $this->DataModel->setUidoutTemRoom($v);//todo 通过UId 剔除临时房间
                    $temRoomData= $this->DataModel->getTemRoomData();
                    $reData['m']="index";
                    $reData['innums']=count($temRoomData);
                    $reData['else']=$nums-count($temRoomData);
                    Gateway::sendToUid($temRoomData,json_encode($reData));
                    return;
                }
            }
            $roomData=$temRoomData;
            $this->DataModel->clearTemRoomData();//todo 清空临时  临时房间
            $roomid=$this->DataModel->getRoomId();//todo 获取房间号
            echo 'roomid='.$roomid;
            $this->DataModel->setRoomAdd();//todo 房间号自增
            $this->DataModel->InitRoomData($roomid,$roomData,$nums);
            $testData=$this->DataModel->getRoomData($roomid);
            echo var_dump($testData);
            $reData['m']="go";
            $reData['innums']=count($temRoomData);
            $reData['else']=$nums-count($temRoomData);
            $reData['roomid']=$roomid;
            Gateway::sendToUid($roomData,json_encode($reData));
        }

    }

    /*todo 放弃匹配
     *
     * 接收客户发送的json匹配数据
     *
     * 从临时房间中抹去
     * 通知房间中的所有客户
     */
    public function stop()
    {
        $uid = $this->SocketData['uid'];
        $nums = $this->SocketData['num'];//多少人的比赛
        $temRoomData = $this->DataModel->getTemRoomData();
        if (in_array($uid, $temRoomData)) {
            $this->DataModel->setUidoutTemRoom($uid);//todo 通过UId 剔除临时房间
            $temRoomData = $this->DataModel->getTemRoomData();
            $reData['m'] = "index";
            $reData['innums'] = count($temRoomData);
            $reData['else'] = $nums - count($temRoomData);
            Gateway::sendToUid($temRoomData, json_encode($reData));
            return;
        }
    }


    /*todo 断线重连
     *todo 传入的参数  roomid：房间号   uid：用户id  num：匹配赛的最大人数
     * 判断当前游戏状态
     *     1.匹配中
     *     2.游戏进入选择页面
     *     3.游戏进入对战页面
     *     允许：加入则返回数据将客户拉入到当前游戏当中
     *     否则：返回数据  使客户端处于未匹配状态
     */
    public function reconnect()//匹配时断线重连
    {
        $uid = $this->SocketData['uid'];
        $roomid = $this->SocketData['roomid'];
        $nums = $this->SocketData['num'];
        $existRoom = $this->DataModel->isExistRoom($roomid);//重连房间是否存在  true 存在  false 不存在
        if ($existRoom == false) {
            //进入匹配页面
        } else {
            $roomData = $this->DataModel->getRoomData($roomid);
            $myGameData = $roomData['userData'][$uid];
            if ($myGameData['out'] == true)  //已经出局
            {
                //进入匹配页面
            } else {
                if ($myGameData['choosegame'] == false) {
                    //进入游戏选择界面
                } else {
                    $gametype = $myGameData['gametype'];
                    switch ($gametype) {
                        case '1':
                            //进入游戏1   :剪刀石头布
                            $goGame ['gametype'] = 1;
                            Gateway::sendToUid($uid, json_encode($goGame));
                            break;
                        case '2':
                            //进入游戏2    开骰子
                            $goGame['gametype'] = 2;
                            Gateway::sendToUid($uid, json_encode($goGame));
                            break;
                        case '3':
                            //进入游戏3
                            $goGame['gametype'] = 3;
                            Gateway::sendToUid($uid, json_encode($goGame));
                            break;
                        case '4':
                            //进入游戏4
                            $goGame['gametype'] = 4;
                            Gateway::sendToUid($uid, json_encode($goGame));
                            break;
                        case '5':
                            //进入游戏5
                            $goGame['gametype'] = 5;
                            Gateway::sendToUid($uid, json_encode($goGame));
                            break;
                        case '6':
                            //进入游戏6
                            $goGame['gametype'] = 6;
                            Gateway::sendToUid($uid, json_encode($goGame));
                            break;
                    }
                }

            }
        }
    }
}

?>